package com.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;

public class JedisUtil {
	static JedisPool pool;
	static Jedis jedis;
	static{
		pool = new JedisPool(new JedisPoolConfig(), "localhost");  
		jedis = pool.getResource(); 
	}
	/**
	 * 发布消息同时入队列
	 * @param channel 频道
	 * @param msg 消息
	 */
	public static long publishAndLpush(String channel,String msg){
		System.out.println("队列："+jedis.lpush(channel+"_list", msg));
		long l = jedis.publish(channel, msg);
		return l;
	}
	/**
	 * 订阅给定频道的信息同时出队列
	 * @param str 订阅的频道
	 * @param callback 处理消息回掉函数
	 */
	public static void subscribeAndRpop(final Callback callback,String...str){
		JedisUtil.jedis.subscribe(new JedisPubSub(){
			@Override
			public void onMessage(final String channel, String message) {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("订阅消息:"+channel+"="+message);
				Jedis jedis = pool.getResource();
				while(true){
					String str = jedis.rpop(channel+"_list");
					if(str == null){
						break;
					}
					callback.onMessage(channel, str);
					System.out.println("队列str:"+str);
				}
				jedis.close();
			}
		}, str);
	}

	/**
	 * 发布普通消息
	 * @param channel 频道
	 * @param msg 消息
	 */
	public static long publish(String channel,String msg){
		long l = jedis.publish(channel, msg);
		return l;
	}
	/**
	 * 订阅给定频道的信息
	 * @param str 订阅的频道
	 * @param callback 处理消息回掉函数
	 */
	public static void subscribe(final Callback callback,String...str){
		JedisUtil.jedis.subscribe(new JedisPubSub(){
			@Override
			public void onUnsubscribe(String channel, int subscribedChannels) {
				System.out.println("onUnsubscribe:"+channel+"="+subscribedChannels);
			}
			@Override
			public void onSubscribe(String channel, int subscribedChannels) {
				System.out.println("onSubscribe:"+channel+"="+subscribedChannels);
			}
			@Override
			public void onPUnsubscribe(String pattern, int subscribedChannels) {
				System.out.println("onPUnsubscribe:"+pattern+"="+subscribedChannels);
			}
			@Override
			public void onPSubscribe(String pattern, int subscribedChannels) {
				System.out.println("onUnsubscribe:"+pattern+"="+subscribedChannels);
			}
			@Override
			public void onPMessage(String pattern, String channel,String message) {
				System.out.println("onUnsubscribe1:"+pattern+"="+channel+"="+message);
			}
			@Override
			public void onMessage(String channel, String message) {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("onUnsubscribe2:"+channel+"="+message);
				callback.onMessage(channel, message);
			}
		}, str);
	}
	/**
	 * 订阅符合给定模式的频道
	 * @param str 订阅的频道
	 * @param callback 处理消息回掉函数
	 */
	public static void psubscribe(final Callback callback,String...str){
		
		JedisUtil.jedis.psubscribe(new JedisPubSub(){
			@Override
			public void onUnsubscribe(String channel, int subscribedChannels) {
				System.out.println("onUnsubscribe:"+channel+"="+subscribedChannels);
			}
			@Override
			public void onSubscribe(String channel, int subscribedChannels) {
				System.out.println("onSubscribe:"+channel+"="+subscribedChannels);
			}
			@Override
			public void onPUnsubscribe(String pattern, int subscribedChannels) {
				System.out.println("onPUnsubscribe:"+pattern+"="+subscribedChannels);
			}
			@Override
			public void onPSubscribe(String pattern, int subscribedChannels) {
				System.out.println("onUnsubscribe:"+pattern+"="+subscribedChannels);
			}
			@Override
			public void onPMessage(String pattern, String channel,String message) {
				System.out.println("onUnsubscribe1:"+pattern+"="+channel+"="+message);
			}
			@Override
			public void onMessage(String channel, String message) {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("onUnsubscribe2:"+channel+"="+message);
				callback.onMessage(channel, message);
			}
		}, str);
	}
	public static void main(String[] args) {
//		JedisUtil ju = new JedisUtil();
//		ju.testString();
//		ju.testMap();
//		ju.testList();
		
//		JedisPool JedisPool1 = new JedisPool("192.168.100.7", 6379);
//		JedisPool JedisPool1 = new JedisPool("192.168.100.25", 7001);
//		JedisPool JedisPool1 = new JedisPool("192.168.100.106", 6379);
//		Jedis jedis = JedisPool1.getResource(); 
		
//		jedis.set("name","minxr");//向key-->name中放入了value-->minxr 
//		System.out.println(jedis.get("name"));//执行结果：minxr 
		long l = JedisUtil.publish("1d3", "1哈哈03");//发布消息
//		System.out.println("订阅者数量："+l);
//		for(int i =0;i<10;i++){
//			long l2 = JedisUtil.publishAndLpush("1d3", "a发布a"+i);//发布消息
//			System.out.println("订阅者数量2："+l2);
//			try {
//				Thread.sleep(20);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//		}
//		jedis.lpush("id3_list", "aa2");
//		List<String> list = jedis.lrange("list", 0, -1);
//		for(String str:list){
//			System.out.println(str+"====");
//		}
//		while(true){
//			String str = jedis.rpop("list");
//			if(str == null){
//				break;
//			}
//			System.out.println("str:"+str);
//			try {
//				Thread.sleep(2000);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//		}

//		JedisUtil.rpop("1d3_list");
//		System.out.println(jedis.rpop("aaa"));
//		System.out.println(jedis.rpop("aaa") == null);
	}

}
